//  YHNetworkManager.m
//  TestYoho
//
//  Created by YiDarren on 17/1/23.
//  Copyright © 2017年 YiDarren. All rights reserved.
//

#import "YHNetworkManager.h"
#import "MBProgressHUD+JDragon.h"
#import "UIProgressView+AFNetworking.h"


@implementation YHNetworkManager

+ (instancetype)manager {
    
    return [[[self class] alloc] initWithBaseURL:nil];
}

+(instancetype)sharedInstance {
    
    static YHNetworkManager *sharedClient = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        sharedClient = [[YHNetworkManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://ssrj.com"]];
        
    });
    return sharedClient;
}

- (instancetype)initWithBaseURL:(NSURL *)url {
    
    self = [super initWithBaseURL:url];
    
    if (self) {
        
        __weak YHNetworkManager *manager = self;
        [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
           
            switch (status) {
                case AFNetworkReachabilityStatusNotReachable:
                    
                    NSLog(@"NotReachable");
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    
                    NSLog(@"WiFi");
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    
                    NSLog(@"WWAN");
                    break;
                case AFNetworkReachabilityStatusUnknown:
                    
                    NSLog(@"Unknow");
                    break;
                default:
                    break;
            }
            
        }];
        [manager.reachabilityManager startMonitoring];
    
    }
    
    return self;
}

- (void)getWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self GET:sessionInfo.URLString parameters:sessionInfo.getParamDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }
        
        [MBProgressHUD showTipMessageInWindow:[error localizedDescription]];
    }];
    
}

- (void)postWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self POST:sessionInfo.URLString parameters:sessionInfo.postParamDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            success(task, responseObject);
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }

        [MBProgressHUD showTipMessageInWindow:[error localizedDescription]];
    }];
}

- (void)putWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self PUT:sessionInfo.URLString parameters:sessionInfo.putParamDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            success(task, responseObject);
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }

        [MBProgressHUD showTipMessageInWindow:[error localizedDescription]];
    }];
}

- (void)patchWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self PATCH:sessionInfo.URLString parameters:sessionInfo.patchParamDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            success(task, responseObject);
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }

        [MBProgressHUD showTipMessageInWindow:[error localizedDescription]];
    }];
}

- (void)deleteWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, id responseObject))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self DELETE:sessionInfo.URLString parameters:sessionInfo.deleteParamDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            success(task, responseObject);
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }

        [MBProgressHUD showTipMessageInWindow:[error localizedDescription]];
    }];
}


- (void)uploadWithSessionInfo:(YHSessionInfo *)sessionInfo success:(void(^)(NSURLSessionDataTask *task, NSError *error))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
    
    sessionInfo.sessionDataTask = [self POST:sessionInfo.URLString parameters:nil  success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }
    }];
    
}

- (void)uploadWithSessionInfo:(YHSessionInfo *)sessionInfo constructingBodyWithBlock:(void(^)(id<AFMultipartFormData> formData))bodyBlock success:(void(^)(NSURLSessionDataTask *task, NSError *error))success failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure {
    
    sessionInfo.sessionDataTask = [self POST:sessionInfo.URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        if (bodyBlock) {
            
            bodyBlock(formData);
        }
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure) {
            
            failure(task, error);
        }
    }];
    
}


@end
